package com.freshmarket.fresh.controller;

import com.freshmarket.fresh.mapper.UserMapper;
import com.freshmarket.fresh.vo.JsonResult;
import com.freshmarket.fresh.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("/vue-admin-template")
public class UserController {
    @Autowired(required = false)
    UserMapper mapper;
    @RequestMapping("/reg")
    public int reg(User user){
        System.out.println("user = "+user);
        if(user.getNumber().length()==0||
            user.getUsername().length()==0||
            user.getPassword().length()==0){
            return 4;
        }
        User u = mapper.selectByUsername(user.getUsername());
        if (u==null){
            User u1 = mapper.selectByNumber(user.getNumber());
           if (u1==null){
               mapper.insert(user);
               return 1;
           }
           return 3;
        }
        return 2;
    }


    // localhost:8088/vue-admin-template/user/login
    @RequestMapping("/user/login")
    public JsonResult getLogin(@RequestBody User user){
        System.out.println("/user/login => " + user);
        HashMap map = new HashMap();
        map.put("token","admin");
        return JsonResult.success(map);
    }

    @RequestMapping("/user/info")
    public JsonResult getUserInfo(){
        HashMap map = new HashMap();
        map.put("roles","[admin]");
        map.put("name","admin");
        map.put("avatar","https://avatar-static.segmentfault.com/153/604/1536043169-61cfdf64be66c_huge256");
        return JsonResult.success(map);
    }

}
